package com.young.shop.admin.controller;


import com.young.shop.admin.service.OrderService;
import com.young.shop.pojo.entity.Order;
import com.young.shop.pojo.vo.BaseRespVO;
import com.young.shop.pojo.vo.OrderServiceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "订单业务")
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("获取订单详情")
    @PostMapping("/selectOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "beforeDate", value = "开始日期", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "afterDate", value = "结束日期", dataType = "String")
    })
    public BaseRespVO info(
            @RequestParam("beforeDate") String beforeDate,
            @RequestParam("afterDate") String afterDate,
            HttpServletRequest request,
            HttpServletResponse response) {

        //跨域
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:63342");
        //获取前端时间
        beforeDate = request.getParameter("beforeDate");
        afterDate = request.getParameter("afterDate");
        System.out.println(beforeDate + "beforeDate");
        try {
            List<Order> orderList = orderService.getOrderList(beforeDate, afterDate);

            return convertSuccessResult(orderList);

        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }

    @ApiOperation("发货")
    @PostMapping("/sendOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "订单id", dataType = "int"),
    })
    public BaseRespVO send(
            @RequestParam("id") int id,
            HttpServletResponse response) {
        System.out.println("进入发货");
        //跨域
        response.setHeader("Access-Control-Allow-Origin", "http://localhost:63342");
        System.out.println(id);
        //通过id修改产品状态,调用services
        try {
            int i = orderService.sendOrder(id, 3);
            return convertSuccessResult(i);

        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }

    @ApiOperation("取消订单")
    @PostMapping("/cancelOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "订单id", dataType = "int"),
    })
    public BaseRespVO cancel(@RequestParam("id") int id,
							 HttpServletResponse response) {

        System.out.println("进入取消订单");
        System.out.println(id);
        //通过id修改产品状态,调用services
        try {
            int i = orderService.cancelOrder(id, 6);
            return convertSuccessResult(i);
        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }

    @ApiOperation("订单详细")
    @PostMapping("/orderDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "订单id", dataType = "int"),
    })
    public BaseRespVO orderDetail(@RequestParam("id") int id,
								  HttpServletResponse response) {

        System.out.println("进入订单详细");
        System.out.println(id);
        //通过id修改产品状态,调用services
        try {
            List<OrderServiceVO> orderServiceVOS = orderService.orderDetail(id);
            return convertSuccessResult(orderServiceVOS);
        } catch (RuntimeException e) {
            String errorMsg = e.getMessage();
            return convertErrorResult(errorMsg);
        }
    }
}
